package com.apps.essentials;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class AddTapLocationOverlay extends ItemizedOverlay<OverlayItem> {
	
	//handles on tap
	Handler handler;
	
    private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();

    private Context context;

    public AddTapLocationOverlay(Drawable defaultMarker) {
         super(boundCenterBottom(defaultMarker));
    }

    public AddTapLocationOverlay(Drawable defaultMarker, Handler h, Context context) {
         this(defaultMarker);
         this.handler = h;
         this.context = context;
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event, MapView mapView)
    {   

        if (event.getAction() == 1) {
            GeoPoint geopoint = mapView.getProjection().fromPixels(
                (int) event.getX(),
                (int) event.getY());
            // latitude
            double lat = geopoint.getLatitudeE6() / 1E6;
            // longitude
            double lon = geopoint.getLongitudeE6() / 1E6;
            //Toast.makeText(context, "Lat: " + lat + ", Lon: "+lon, Toast.LENGTH_SHORT).show();
        }
        return false;
    } 

    @Override
    protected OverlayItem createItem(int i) {
       return mapOverlays.get(i);
    }

    @Override
    public int size() {
       return mapOverlays.size();
    }

    @Override
    protected boolean onTap(int index) {
      OverlayItem item = mapOverlays.get(index);
      AlertDialog.Builder dialog = new AlertDialog.Builder(this.context);
      dialog.setTitle(item.getTitle());
      dialog.setMessage(item.getSnippet());
      dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});
      
      dialog.show();
      return true;
    }
    
	// This method is invoked, when user tap on the map
	@Override
	public boolean onTap(GeoPoint p, MapView map) {		
       
	    List<Overlay> overlays = map.getOverlays();
	    
	    // Creating a Message object to send to Handler
	    Message message = new Message();
	    
	    // Creating a Bundle object ot set in Message object
	    Bundle data = new Bundle();
	    
	    // Setting latitude in Bundle object
	    data.putInt("latitude", p.getLatitudeE6());
	    //data.putDouble("latitude", p.getLatitudeE6());
	    // Setting longitude in the Bundle object
	    data.putInt("longitude", p.getLongitudeE6());
	   //data.putDouble("longitude", p.getLongitudeE6());
	    // Setting the Bundle object in the Message object
	    message.setData(data);
	    
	    // Sending Message object to handler
	    handler.sendMessage(message);		
 
		return super.onTap(p, map);
	}    
    public void addOverlay(OverlayItem overlay) {
       mapOverlays.add(overlay);
    }
    
    public void populateNow(){
 	   this.populate();
    }

 }